package edu.sctu.xiangkun.day201506.day20150614.textthree;


import java.io.*;

/**
 * Created by user21 on 2015/6/14.
 */
public class IODemo {
    FileReader filereader;
    FileWriter fileWriter;
    public void fileReader() throws IOException {
        int countbyte = 0;
        try{
            filereader = new FileReader(new File("D://xk.txt"));
            fileWriter = new FileWriter(new File("D://xk1.txt"));

            try{
                while((countbyte = filereader.read()) != -1){
                    fileWriter.write(countbyte);
                    System.out.print((char)countbyte);
                }

            }catch(IOException e){
                e.printStackTrace();
            }

            try{
                filereader.close();
                fileWriter.close();
            }catch(IOException e){
                e.printStackTrace();
            }

        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
    }

    public void fileInputString(){
        FileInputStream fileInputStream;
        FileOutputStream fileOutputStream;
        int count = 0;
        int countbyte = 0;
        byte[] buffer = new byte[1024];
        try{
            fileInputStream = new FileInputStream(new File("D://xk.txt"));
            fileOutputStream = new FileOutputStream(new File("D://xk2.txt"));

            try{
                while((countbyte = fileInputStream.read(buffer)) != -1){
                    //将字节转换为字符串
                    String str = new String(buffer);
                    System.out.println("\n"+str);
                    //将字符串转换为字符数组输出
                    char[] chars = str.toCharArray();
                    System.out.println(chars);
                    count += countbyte;
                    fileOutputStream.write(buffer,0,count);
                }
                System.out.println(count);
            }catch(IOException e){
                e.printStackTrace();
            }

            try{
                fileInputStream.close();
                fileOutputStream.close();
            }catch(IOException e){
                e.printStackTrace();
            }

        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
    }
}
